/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.elka.gis.main.node;

import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author looser
 */
public class Node implements Comparable{
    private Integer x;
    private Integer y;
    private String name;
    Set<Node> availableNodes;

    public Node() {
    }

    public Node(String name, Integer x, Integer y) {
        this.x = x;
        this.y = y;
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

    public Set<Node> getAvailableNodes() {
        return availableNodes;
    }

    public void setAvailableNodes(Set<Node> availableNodes) {
        this.availableNodes = availableNodes;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Node other = (Node) obj;
        if (this.x != other.x && (this.x == null || !this.x.equals(other.x))) {
            return false;
        }
        if (this.y != other.y && (this.y == null || !this.y.equals(other.y))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.x != null ? this.x.hashCode() : 0);
        hash = 59 * hash + (this.y != null ? this.y.hashCode() : 0);
        hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    public void addAvailableNode(Node node){
        if(availableNodes==null){
            availableNodes = new HashSet<Node>();
        }
        availableNodes.add(node);
    }

    @Override
    public int compareTo(Object t) {
        if (t.getClass().getName().equals(this.getClass().getName())) {
            Node n = (Node) t;
            if (this.getAvailableNodes().size() > n.getAvailableNodes().size())
                return -1;
            else if (this.getAvailableNodes().size() < n.getAvailableNodes().size())
                return 1;
            else if (this.getName().equals(n.getName()))
                return 0;
            else
                return 1;
        }
        else
            return 0;
    }

    @Override
    public String toString() {
        if (this.name!=null)
            return name;
        else
           return "";
    }
}
